package org.study.util;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-08-09
 */
public class CollectionUtils {
    public static void main(String[] args) {
        /*List<String> srcList = new ArrayList<String>() {{
            for (int i = 0; i < 15; i++) {
                add("src-list-" + i);
            }
        }};
        List<List<String>> splitList = splitList(srcList, 5);
        System.out.println("splitList.size()=" + splitList.size());
        splitList.forEach(item -> System.out.println(JSON.toJSONString(item)));

        List<List<String>> averageAssign = averageAssign(srcList, 5);
        System.out.println("averageAssign.size()=" + averageAssign.size());
        averageAssign.forEach(item -> System.out.println(JSON.toJSONString(item)));*/

        List<String> moidList1 = new ArrayList<>();
        moidList1.add("1");
        moidList1.add("2");
        moidList1.add("2");
        moidList1.add("3");
        moidList1.add("3");
        moidList1.add("3");
        moidList1.add("4");
        moidList1.add("4");
        moidList1.add("4");
        moidList1.add("4");
        Map<String, Long> map1 = moidList1.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println("moid出现次数统计（moid=次数）：" + map1);
        List<Map.Entry<String, Long>> list1 = new ArrayList<>(map1.entrySet());
        Collections.sort(list1, new Comparator<Map.Entry<String, Long>>() {
            @Override
            public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
                //按照value值，从小到大排序
//                return o1.getValue() - o2.getValue();
                //按照value值，从大到小排序
//                return o2.getValue() - o1.getValue();
                //按照value值，用compareTo()方法默认是从小到大排序
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        for (Map.Entry s : list1) {
            System.out.println("出现次数最多的moid：" + s.getKey() + "--" + s.getValue());
        }
    }

    /**
     * 按指定大小分割集合
     *
     * @param srcList 待分割的list
     * @param len     分割后每个list大小
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> srcList, int len) {
        if (srcList == null || srcList.size() == 0 || len < 1) {
            return null;
        }
        List<List<T>> result = new ArrayList<>();
        int size = srcList.size();
        int count = (size + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<T> subList = srcList.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
            result.add(subList);
        }
        return result;
    }

    /**
     * 将一个list均分成多个list
     *
     * @param srcList 待分割的list
     * @param number  要分割成几个list
     * @return
     */
    public static <T> List<List<T>> averageAssign(List<T> srcList, int number) {
        List<List<T>> result = new ArrayList<>();
        int remainder = srcList.size() % number;
        int aliquot = srcList.size() / number;
        int offset = 0;
        for (int i = 0; i < number; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = srcList.subList(i * aliquot + offset, (i + 1) * aliquot + offset + 1);
                remainder--;
                offset++;
            } else {
                value = srcList.subList(i * aliquot + offset, (i + 1) * aliquot + offset);
            }
            result.add(value);
        }
        return result;
    }
}
